Page({
  data: {
    dialogVisible: false,
    houseList: [],
  },

  swipeClose(ev) {
    const { position, instance } = ev.detail;

    if (position === "right") {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      });
      // 把当前标签的参数获取到记录下来，id  index
      this.cellIndex = ev.mark.index;
      this.cellId = ev.mark.id;

      // swiper-cell 滑块关闭
      instance.close();
    }
  },

  goDetail(ev) {
    wx.navigateTo({
      url: "/house_pkg/pages/detail/index?id=" + ev.mark.id,
    });
  },

  addHouse() {
    wx.navigateTo({
      url: "/house_pkg/pages/locate/index",
    });
  },
  onShow() {
    this.getList();
  },
  async getList() {
    const { code, data } = await wx.http.get("/room");
    if (code !== 10000) return wx.utils.toast();
    this.setData({
      houseList: data,
    });
    // console.log(res);
  },
  async dialogClose(ev) {
    // console.log(ev);
    if (ev.detail === "confirm") {
      // 删除
      const { code } = await wx.http.delete("/room/" + this.cellId);
      if (code !== 10000) return wx.utils.toast("删除失败");
      // 页面上的展示也需要发生变化
      this.data.houseList.splice(this.cellIndex, 1);
      // 删除数据之后，再重新setDta一下 >> 页面更新
      this.setData({
        houseList: this.data.houseList,
      });
    }
  },
});
